package com.cuz.hot100;

public class Q6zConvert {

    public static void main(String[] args) {
//        System.out.println(convert1("PAYPALISHIRING", 4));
//        System.out.println(convert1("PAYPALISHIRING", 3));
        System.out.println(convert1("AB", 1));
    }

    public static String convert1(String s, int numRows) {
        if (s == null || s.length() == 0 || numRows <= 1) {
            return s;
        }
        char[] charArray = s.toCharArray();
        int len = s.length();
        StringBuilder[] resBuf = new StringBuilder[numRows];
        for (int index = 0; index < resBuf.length; index++) {
            resBuf[index] = new StringBuilder();
        }
        int cur = 0;
        for (int index = 0; index < len; ) {
            while (cur < numRows) {
                if (index >= len) {
                    break;
                }
                resBuf[cur].append(charArray[index++]);
                cur++;
            }
            cur -= 2;
            while (cur >= 0) {
                if (index >= len) {
                    break;
                }
                resBuf[cur].append(charArray[index++]);
                cur--;
            }
            cur = 1;
        }
        StringBuilder res = new StringBuilder();
        for (StringBuilder temp : resBuf) {
            res.append(temp);
        }
        return res.toString();
    }
}
